package cn.jekun.shop.common.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.NotBlank;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpBarcodeImage;
import cn.jekun.component.core.bean.gen.ErpHotSearch;
import cn.jekun.component.core.bean.gen.ErpInventory;
import cn.jekun.component.core.bean.gen.ErpMerchandise;
import cn.jekun.component.core.bean.gen.ErpMerchandiseImage;
import cn.jekun.component.core.bean.gen.ErpMerchandiseType;
import cn.jekun.component.core.bean.gen.ErpSalePrice;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.ErpShoptype;
import cn.jekun.component.core.service.gen.ErpBarcodeImageService;
import cn.jekun.component.core.service.gen.ErpHotSearchService;
import cn.jekun.component.core.service.gen.ErpInventoryService;
import cn.jekun.component.core.service.gen.ErpMerchandiseImageService;
import cn.jekun.component.core.service.gen.ErpMerchandiseService;
import cn.jekun.component.core.service.gen.ErpMerchandiseTypeService;
import cn.jekun.component.core.service.gen.ErpSalePriceService;
import cn.jekun.component.shop.bean.Goods;

/**
 * 
 * @author twj
 *
 */
@Service(ShopGoodService.BEAN_NAME)
public class ShopGoodService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.common.service.ShopGoodService";

	@Resource(name = ErpMerchandiseService.BEAN_NAME)
	private ErpMerchandiseService erpMerchandiseService;
	
	@Resource(name = ErpMerchandiseImageService.BEAN_NAME)
	private ErpMerchandiseImageService erpMerchandiseImageService;
	
	@Resource(name=ErpInventoryService.BEAN_NAME)
	private ErpInventoryService erpInventoryService;
	
	@Resource(name=ErpSalePriceService.BEAN_NAME)
	private ErpSalePriceService erpSalePriceService;
	
	@Resource(name=ErpMerchandiseTypeService.BEAN_NAME)
	private ErpMerchandiseTypeService erpMerchandiseTypeService;
	
	@Resource(name = ErpBarcodeImageService.BEAN_NAME)
	private ErpBarcodeImageService erpBarcodeImageService;
	
	@Resource(name = ErpHotSearchService.BEAN_NAME)
	private ErpHotSearchService erpHotSearchService;
	
	@Override
	public String getNamespace() {
		return "SHOP_GOOD";
	}
	
    /**
     * 商家商品管理的商品列表接口
     * @param shopId 商家代码
     * @param typeId 商家自定义商品分类 0 全部
     * @param filter 筛选(2全部、1上架、0下架、3有图、4无图、5推荐、6非推荐)
     * @param sort 排序 0全部、 1销量最高、2销量最低、3格最高、4价格最低
     * @param page 分页
     * @return
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public JSONObject goodList(
			@NotBlank(message="商家代码不能为空")
			@ErpShop.AnnoShopId
			String shopId,
			@ErpShoptype.AnnoShopTypeId
			String typeId,
			@Security
			String filter,
			@Security String sort,
			Page page) {
		JSONObject jsonObject = new JSONObject();
		try {
			Map map = new HashMap();
			map.put("shopId", shopId);
			map.put("typeId", typeId);
			//筛选下架
			if ("0".equals(filter)) {
				map.put("closed", Constants.GOODS_CLOSED);
			}
			//筛选上架
			if ("1".equals(filter)) {
				map.put("closed", Constants.GOODS_OPEN);
			}
			//筛选有图
			if ("3".equals(filter)) {
				map.put("hasImage", "1");
			}
			//筛选无图
			if ("4".equals(filter)) {
				map.put("hasImage", "0");
			}
			//筛选推荐
			if ("5".equals(filter)) {
				map.put("recommended", "1");
			}
			//筛选非推荐
			if ("6".equals(filter)) {
				map.put("recommended", "0");
			}
			
			map.put("sort", sort);
			List<Goods> goods = this.list(map , page);
			jsonObject.put("goods", JSONArray.fromObject(goods));
			jsonObject.put("totals", page.getTotalCount());
			jsonObject.put("pages", page.getPages());
			jsonObject.put("pageSize", page.getPageSize());
			jsonObject.put("currentPage", page.getPageNumber());
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
		
	}
	/**
     * 商家商品管理的搜索商品接口
     * @param name 商品名称
     * @param page 分页
     * @return
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public JSONObject searchGoods(
			@NotBlank(message="商家代码不能为空")
			@ErpMerchandise.AnnoShopId
			String shopId,
			
			@Security
			String name,
			Page page,
			@Security
			String barCode) {
		JSONObject jsonObject = new JSONObject();
		try {
			Map map = new HashMap();
			map.put("shopId", shopId);
			map.put("name", name);
			map.put("barCode", barCode);
			List<Goods> goods = this.list(this.getStatementName("search"),map , page);
			jsonObject.put("goods", JSONArray.fromObject(goods));
			jsonObject.put("totals", page.getTotalCount());
			jsonObject.put("pages", page.getPages());
			jsonObject.put("pageSize", page.getPageSize());
			jsonObject.put("currentPage", page.getPageNumber());
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
		
	}
	
	
	/**
     * 关键词搜索接口
     * @param page 分页
     * @return
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public JSONObject searchByHot(
			Page page) {
		JSONObject jsonObject = new JSONObject();
		try {
			Map map = new HashMap();
			List<ErpHotSearch> hotSearchs = this.erpHotSearchService.list(map,page);
			jsonObject.put("hotSearchs", JSONArray.fromObject(hotSearchs));
			jsonObject.put("totals", page.getTotalCount());
			jsonObject.put("pages", page.getPages());
			jsonObject.put("pageSize", page.getPageSize());
			jsonObject.put("currentPage", page.getPageNumber());
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
		
	}

    /**
     * 商家商品管理的商品上下架设置接口
     * @param userId 用户代码
     * @param merchandiseId 商品代码
     * @param online 上架下架(1上架、0下架)
     * @return
     */
	@Transactional
	public JSONObject online(
			@Security
			String userId,
			@NotBlank(message="商品代码不能为空")
			@ErpMerchandise.AnnoMerchandiseId
			String merchandiseId,
			@Security
			@NotBlank(message="商品状态不能为空")
			String online) {
		JSONObject jsonObject = new JSONObject();
		try {
			ErpMerchandise erpMerchandise = new ErpMerchandise();
			erpMerchandise.setMerchandiseId(merchandiseId);
			erpMerchandise.setClosed(online);
			erpMerchandise.setUpdateName(userId);
			this.erpMerchandiseService.update(erpMerchandise);
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
		
	}
	
	/**
     * 商家商品管理的商品上下架设置接口
     * @param userId 用户代码
     * @param merchandiseId 商品代码
     * @param recommended 推荐(1是0否)
     * @return
     */
	@Transactional
	public JSONObject recommended(
			@Security
			String userId,
			@NotBlank(message="商品代码不能为空")
			@ErpMerchandise.AnnoMerchandiseId
			String merchandiseId,
			@Security
			@NotBlank(message="推荐不能为空")
			String recommended) {
		JSONObject jsonObject = new JSONObject();
		try {
			ErpMerchandise erpMerchandise = new ErpMerchandise();
			erpMerchandise.setMerchandiseId(merchandiseId);
			erpMerchandise.setRecommended(recommended);
			erpMerchandise.setUpdateName(userId);
			this.erpMerchandiseService.update(erpMerchandise);
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
		
	}
	/**
	 * 商家商品管理的商品信息
	 * @param merchandiseId 商品代码
	 * @return
	 */
	public JSONObject getGoodDetail(
			@NotBlank(message="商品代码不能为空")
			@ErpMerchandise.AnnoMerchandiseId
			String merchandiseId) {
		JSONObject jsonObject = new JSONObject();
		try {
			Map map = new HashMap();
			map.put("merchandiseId", merchandiseId);
			Goods goods = this.one(this.getStatementName("goodDetail"),map);
			jsonObject = JSONObject.fromObject(goods);
			//商品图片
			Map imageMap = new HashMap();
			imageMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandiseId);
			SqlUtis.prepareSql(imageMap, 
					SqlUtis.getSQL(imageMap, ErpMerchandiseImage.COL_GOODS_ID),
					SqlUtis.orderBy(ErpMerchandiseImage.COL_SORT)
					);
			List<ErpMerchandiseImage> images = this.erpMerchandiseImageService.list(imageMap);
			
			jsonObject.put("images", JSONArray.fromObject(images));
			
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
	}
	/**
	 * 商家商品管理的扫描条码时获取商品信息(如果商家有该商品，则返回该商品的信息(修改已有商品)，没有则从条码库里获取(新增商品))
	 * @param shopId 商品代码
	 * @param barcode 条形码
	 * @return
	 */
	public JSONObject getGoodDetailByBarcode(
			@NotBlank(message="商家代码不能为空")
			@ErpMerchandise.AnnoShopId
			String shopId,
			
			@NotBlank(message="条形码不能为空")
			@ErpMerchandise.AnnoBarcode
			String barcode
			) {
		JSONObject jsonObject = new JSONObject();
		try {
			Map map = new HashMap();
			map.put("shopId", shopId);
			map.put("barcode", barcode);
			Goods goods = this.one(this.getStatementName("goodDetailByBarcode"),map);
			if (goods!=null) {
				//商品图片
				Map imageMap = new HashMap();
				imageMap.put(ErpMerchandiseImage.COL_GOODS_ID, goods.getMerchandiseId());
				SqlUtis.prepareSql(imageMap, 
						SqlUtis.getSQL(imageMap, ErpMerchandiseImage.COL_GOODS_ID),
						SqlUtis.orderBy(ErpMerchandiseImage.COL_SORT)
						);
				List<ErpMerchandiseImage> images = this.erpMerchandiseImageService.list(imageMap);
				
				jsonObject = JSONObject.fromObject(goods);
				jsonObject.put("images", JSONArray.fromObject(images));
			}else{
				goods = this.one(this.getStatementName("getBarcode"),map);
				if(goods!=null){
					//商品图片
					Map imageMap = new HashMap();
					imageMap.put(ErpBarcodeImage.COL_BARCODE, goods.getBarcode());
					SqlUtis.prepareSql(imageMap, 
							SqlUtis.getSQL(imageMap, ErpBarcodeImage.COL_BARCODE),
							SqlUtis.orderBy(ErpMerchandiseImage.COL_SORT)
							);
					List<ErpBarcodeImage> images = this.erpBarcodeImageService.list(imageMap);
					
					jsonObject = JSONObject.fromObject(goods);
					jsonObject.put("images", JSONArray.fromObject(images));
				}else{
					
					jsonObject.put(ErrorMsg.KEY_CODE, 2);
					jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
					return jsonObject;//没有数据
				}
			}
			
			return this.executeRerutn(jsonObject);
		} catch (Exception e) {
			return this.executeRerutnFail(jsonObject);
		}
	}

	/**
	 * 增加商品
	 * @param shopId
	 * @param merchandiseTypeId
	 * @param shopMerchandiseTypeId
	 * @param merchandiseName
	 * @param price
	 * @param mktprice
	 * @param cost
	 * @param inventory
	 * @param brief
	 * @param closed
	 * @param barcode
	 * @param imageUrl
	 * @param userId
	 * @return
	 */
	@Transactional
	public JSONObject addGood(
			@NotBlank(message = "商家代码不能为空") @ErpMerchandise.AnnoShopId String shopId,
			@ErpMerchandise.AnnoMerchandiseTypeId String merchandiseTypeId,
			@ErpMerchandise.AnnoShopMerchandiseTypeId String shopMerchandiseTypeId,
			@NotBlank(message = "商品名称不能为空") @ErpMerchandise.AnnoMerchandiseName String merchandiseName,
			@NotBlank(message = "商品销售价格不能为空") @ErpSalePrice.AnnoPrice String price,
			@ErpMerchandise.AnnoMktprice String mktprice,
			@ErpSalePrice.AnnoCost String cost,
			@Security String inventory,
			@ErpMerchandise.AnnoBrief String brief,
			@NotBlank(message = "商品上架不能为空") @ErpMerchandise.AnnoClosed String closed,
			@ErpMerchandise.AnnoBarcode String barcode,
			@Security String imageUrl,
			@Security String userId,
			@Security String merchandiseWeight,
			@Security String unitName,
			@Security String recommended
			) {
		JSONObject jsonObject = new JSONObject();
		// TODO 商家商品流水表

		// 是否存在相同的条形码
		if (StringUtils.isNotBlank(barcode)) {
			HashMap existsMap = new HashMap();
			existsMap.put(ErpMerchandise.COL_SHOP_ID, shopId);
			existsMap.put(ErpMerchandise.COL_BARCODE, barcode);
			SqlUtis.prepareSql(existsMap, SqlUtis.getSQLRequired(existsMap, ErpMerchandise.COL_SHOP_ID), SqlUtis.getSQLRequired(existsMap, ErpMerchandise.COL_BARCODE));
			if (this.erpMerchandiseService.exists(existsMap)) {// 商家已经存在相同的条形码
				throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE, "已经存在相同的条形码");
			}
		}else{
			barcode =(""+ System.currentTimeMillis());//没有填条形码，自动生成一个
		}
		// 增加商品
		ErpMerchandise erpMerchandise = new ErpMerchandise();
		erpMerchandise.setShopId(shopId);
		erpMerchandise.setMerchandiseTypeId(merchandiseTypeId);
		erpMerchandise.setShopMerchandiseTypeId(shopMerchandiseTypeId);
		erpMerchandise.setMerchandiseName(merchandiseName);
		if(!StringUtils.isBlank(mktprice)){
			erpMerchandise.setMktprice(Double.parseDouble(mktprice));
		}
		erpMerchandise.setBrief(brief);
		erpMerchandise.setBarcode(barcode);
		erpMerchandise.setClosed(closed);
		erpMerchandise.setCreateName(userId);
		erpMerchandise.setVirtual(Constants.GOODS_UNVIRTUAL);// 默认普通商品
		erpMerchandise.setUnitName(unitName);
		erpMerchandise.setRecommended(recommended);
		erpMerchandise.setSeoWeight(StringUtils.isBlank(merchandiseWeight)?0:Double.parseDouble(merchandiseWeight));//权重

		String merchandiseId = (String) this.erpMerchandiseService.insert(erpMerchandise);// 增加商品

		// 增加库存
		if(StringUtils.isBlank(inventory)){
			
			inventory="0";
		}
		ErpInventory erpInventory = new ErpInventory();
		erpInventory.setMerchandiseId(merchandiseId);
		erpInventory.setInventory(Integer.parseInt(inventory));
		erpInventory.setCreateName(userId);
		this.erpInventoryService.insert(erpInventory);
		// 增加价格
		ErpSalePrice erpSalePrice = new ErpSalePrice();
		erpSalePrice.setMerchandiseId(merchandiseId);
		if(!StringUtils.isBlank(cost)){
			erpSalePrice.setCost(Double.parseDouble(cost));
		}
		if(!StringUtils.isBlank(price)){
			erpSalePrice.setPrice(Double.parseDouble(price));
		}
		
		erpSalePrice.setCreateName(userId);
		this.erpSalePriceService.insert(erpSalePrice);
		//商品图片
		if (StringUtils.isNotBlank(imageUrl)) {
			String[] images = imageUrl.split(",");
			for (int i=0;i<images.length;i++) {
				ErpMerchandiseImage erpMerchandiseImage = new ErpMerchandiseImage();
				erpMerchandiseImage.setCreateName(userId);
				erpMerchandiseImage.setDef(i==0?"1":"0");//第一张作为默认图片
				erpMerchandiseImage.setGoodsId(merchandiseId);
				erpMerchandiseImage.setImageUrl(images[i]);
				erpMerchandiseImage.setSort(i);//排序
				this.erpMerchandiseImageService.insert(erpMerchandiseImage);
			}
		}
		jsonObject.put("merchandiseId", merchandiseId);
		return this.executeRerutn(jsonObject);
	}

	/**
	 * 修改商品
	 * @param shopId
	 * @param merchandiseId
	 * @param merchandiseTypeId
	 * @param shopMerchandiseTypeId
	 * @param merchandiseName
	 * @param price
	 * @param mktprice
	 * @param cost
	 * @param inventory
	 * @param brief
	 * @param closed
	 * @param barcode
	 * @param imageUrl
	 * @param userId
	 * @return
	 */
	@Transactional
	public JSONObject editGood(
			@NotBlank(message = "商家代码不能为空") @ErpMerchandise.AnnoShopId String shopId,
			@NotBlank(message = "商品不能为空") @ErpMerchandise.AnnoMerchandiseId String merchandiseId,
			@ErpMerchandise.AnnoMerchandiseTypeId String merchandiseTypeId,
			@ErpMerchandise.AnnoShopMerchandiseTypeId String shopMerchandiseTypeId,
			@NotBlank(message = "商品名称不能为空") @ErpMerchandise.AnnoMerchandiseName String merchandiseName,
			@NotBlank(message = "商品销售价格不能为空") @ErpSalePrice.AnnoPrice String price,
			@ErpMerchandise.AnnoMktprice String mktprice,
			@ErpSalePrice.AnnoCost String cost,
			@Security String inventory,
			@ErpMerchandise.AnnoBrief String brief,
			@NotBlank(message = "商品上架不能为空") @ErpMerchandise.AnnoClosed String closed,
			@ErpMerchandise.AnnoBarcode String barcode,
			@Security String imageUrl,
			@Security String userId,
			@Security String merchandiseWeight,
			@Security String unitName,
			@Security String recommended
			) {
		JSONObject jsonObject = new JSONObject();
		// TODO 商家商品流水表

		// 是否存在相同的条形码
		if (StringUtils.isNotBlank(barcode)) {
			ErpMerchandise oldErpMerchandise=this.erpMerchandiseService.one(merchandiseId);
			if (!barcode.equals(oldErpMerchandise.getBarcode())) {
				HashMap existsMap = new HashMap();
				existsMap.put(ErpMerchandise.COL_SHOP_ID, shopId);
				existsMap.put(ErpMerchandise.COL_BARCODE, barcode);
				SqlUtis.prepareSql(existsMap, SqlUtis.getSQLRequired(existsMap, ErpMerchandise.COL_SHOP_ID), SqlUtis.getSQLRequired(existsMap, ErpMerchandise.COL_BARCODE));
				if (this.erpMerchandiseService.exists(existsMap)) {// 商家已经存在相同的条形码
					throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE, "已经存在相同的条形码");
				}
			}
		}
		// 修改商品
		ErpMerchandise erpMerchandise = new ErpMerchandise();
		erpMerchandise.setMerchandiseId(merchandiseId);
		erpMerchandise.setMerchandiseTypeId(merchandiseTypeId);
		erpMerchandise.setShopMerchandiseTypeId(shopMerchandiseTypeId);
		erpMerchandise.setMerchandiseName(merchandiseName);
		if(!StringUtils.isBlank(mktprice)){
			erpMerchandise.setMktprice(Double.parseDouble(mktprice));
		}

		if(!StringUtils.isBlank(merchandiseWeight)){//权重
			erpMerchandise.setSeoWeight(Double.parseDouble(merchandiseWeight));
		}

		erpMerchandise.setBrief(brief);
		erpMerchandise.setBarcode(barcode);
		erpMerchandise.setClosed(closed);
		erpMerchandise.setUpdateName(userId);
		erpMerchandise.setUnitName(unitName);
		erpMerchandise.setRecommended(recommended);
		this.erpMerchandiseService.update(erpMerchandise);// 修改商品

		// 修改库存
		if(StringUtils.isBlank(inventory)){
			
			inventory="0";
		}
		
			
			Map erpInventoryMap = new  HashMap();
			erpInventoryMap.put(ErpInventory.COL_MERCHANDISE_ID, merchandiseId);
			SqlUtis.prepareSql(erpInventoryMap,SqlUtis.getSQL(erpInventoryMap, ErpInventory.COL_MERCHANDISE_ID));
			
			
			if(this.erpInventoryService.exists(erpInventoryMap)){
				
				ErpInventory erpInventory = new ErpInventory();
				erpInventory.setInventory(Integer.parseInt(inventory));
				erpInventory.setUpdateName(userId);
				erpInventoryMap.put("erpInventory", erpInventory);
				this.erpInventoryService.update(erpInventoryMap);
				
			}else{
				
				ErpInventory erpInventory = new ErpInventory();
				erpInventory.setInventory(Integer.parseInt(inventory));
				erpInventory.setUpdateName(userId);
				erpInventory.setMerchandiseId(merchandiseId);
				erpInventoryMap.put("erpInventory", erpInventory);
				this.erpInventoryService.insert(erpInventoryMap);
				
			}
		// 修改价格
		
		
		Map erpSalePriceMap = new  HashMap();
		erpSalePriceMap.put(ErpSalePrice.COL_MERCHANDISE_ID, merchandiseId);
		SqlUtis.prepareSql(erpSalePriceMap,SqlUtis.getSQL(erpSalePriceMap, ErpInventory.COL_MERCHANDISE_ID));
		
		
		ErpSalePrice erpSalePrice = new ErpSalePrice();
		if(!StringUtils.isBlank(cost)){
			erpSalePrice.setCost(Double.parseDouble(cost));
		}
		if(!StringUtils.isBlank(price)){
			erpSalePrice.setPrice(Double.parseDouble(price));
		}
		
		erpSalePrice.setUpdateName(userId);
		
		
		if(this.erpSalePriceService.exists(erpSalePriceMap)){
			erpSalePriceMap.put("erpSalePrice", erpSalePrice);

			this.erpSalePriceService.update(erpSalePriceMap);
		}else{
			erpSalePrice.setMerchandiseId(merchandiseId);
			erpSalePriceMap.put("erpSalePrice", erpSalePrice);
			this.erpSalePriceService.insert(erpSalePriceMap);
		}

		//商品图片
		Map deleteImageMap = new HashMap();
		deleteImageMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandiseId);
		SqlUtis.prepareSql(deleteImageMap, SqlUtis.getSQLRequired(deleteImageMap, ErpMerchandiseImage.COL_GOODS_ID));
		this.erpMerchandiseImageService.delete(deleteImageMap);
		
		if (StringUtils.isNotBlank(imageUrl)) {
			String[] images = imageUrl.split(",");
			for (int i=0;i<images.length;i++) {
				ErpMerchandiseImage erpMerchandiseImage = new ErpMerchandiseImage();
				erpMerchandiseImage.setCreateName(userId);
				erpMerchandiseImage.setDef(i==0?"1":"0");//第一张作为默认图片
				erpMerchandiseImage.setGoodsId(merchandiseId);
				erpMerchandiseImage.setImageUrl(images[i]);
				erpMerchandiseImage.setSort(i);//排序
				this.erpMerchandiseImageService.insert(erpMerchandiseImage);
			}
		}
		
		return this.executeRerutn(jsonObject);
	}

	/**
	 * 增加商家自定义商品分类
	 * @param shopId 商家代码
	 * @param parentId 上级分类代码
	 * @param typeName 分类名称
	 * @param userId
	 * @return
	 */
	@Transactional
	public JSONObject addType(
			@NotBlank(message="商家代码不能为空")
			@ErpMerchandiseType.AnnoShopId
			String shopId,
			
			@ErpMerchandiseType.AnnoParentId
			String parentId, 
			
			@NotBlank(message="商品分类名称不能为空")
			@ErpMerchandiseType.AnnoMerchandiseTypeName
			String typeName, 
			
			@Security
			String userId) {
		JSONObject jsonObject = new JSONObject();
		ErpMerchandiseType erpMerchandiseType = new ErpMerchandiseType();
		erpMerchandiseType.setShopId(shopId);
		erpMerchandiseType.setMerchandiseTypeName(typeName);
		erpMerchandiseType.setParentId(parentId);
		erpMerchandiseType.setMemo(userId+"自定义分类");
		String merchandiseTypeId = (String)erpMerchandiseTypeService.insert(erpMerchandiseType);
		jsonObject.put("typeId", merchandiseTypeId);
		return this.executeRerutn(jsonObject);
	}

	/**
	 * 修改商家自定义商品分类
	 * @param typeId 分类代码
	 * @param typeName 分类名称
	 * @param userId
	 * @return
	 */
	@Transactional
	public JSONObject editType(
			@NotBlank(message="商品分类代码不能为空")
			@ErpMerchandiseType.AnnoMerchandiseTypeId
			String typeId,
			
			@NotBlank(message="商品分类名称不能为空")
			@ErpMerchandiseType.AnnoMerchandiseTypeName
			String typeName,
			
			@Security
			String userId) {
		JSONObject jsonObject = new JSONObject();
		ErpMerchandiseType erpMerchandiseType = new ErpMerchandiseType();
		erpMerchandiseType.setMerchandiseTypeId(typeId);
		erpMerchandiseType.setMerchandiseTypeName(typeName);
		erpMerchandiseTypeService.update(erpMerchandiseType);
		return this.executeRerutn(jsonObject);
	}
    
}
